<template>
  <div class="wrapper">
    <div class="login">
      <div class="avatar-box">
        <img src="../assets/logo.png" alt="头像">
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
            class="account"
            placeholder="请输入您的账号"
            size="normal"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            class="password"
            placeholder="请输入您的密码"
            size="normal"
            type="password"
          />
        </el-form-item>

        <el-form-item class="btns">
          <el-button
            type="primary"
            size="default"
            @click="login"
          >登录</el-button>
          <el-button type="primary" size="info" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- <div class="input-info">
        <el-input v-model="account" prefix-icon="el-icon-user-solid" class="account" placeholder="请输入您的账号" size="normal" />
        <el-input v-model="password" prefix-icon="el-icon-lock" class="password" placeholder="请输入您的密码" size="normal" />
      </div> -->

      <!-- <div class="btn">
        <el-button type="primary" size="default" @click="login">登录</el-button>
        <el-button type="primary" size="info" @click="reset">重置</el-button>
      </div> -->
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '用户名不能为空！', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空！', trigger: 'blur' },
          {
            min: 3,
            max: 15,
            message: '密码长度在 3 到 15 个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginFormRef.validate(async(valid) => {
        if (!valid) return

        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        const token = res.data.token
        window.sessionStorage.setItem('token', token)
        this.$router.push('/home')
      })
    },
    reset() {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.wrapper {
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;

  .login {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: relative;

    .avatar-box {
      width: 130px;
      height: 130px;
      position: absolute;
      // left: 130px;
      // top: -70px;
      left: 50%;
      transform: translate(-50%, -50%);
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      // overflow: hidden;
      background-color: #fff;
      box-shadow: 0 0 10px #ddd;

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }

    .login_form {
      position: absolute;
      bottom: 0;
      width: 100%;
      // form表单默认为的默认样式为 box-sizing: content
      // 所以需要修改成 'border-box'，这样padding才会正常生效
      padding: 0 20px;
      box-sizing: border-box;
    }

    .btns {
      display: flex;
      justify-content: center;
    }

    // .input-info {
    //   width: 100%;
    //   position: absolute;
    //   top: 120px;

    //   .account,
    //   .password {
    //     padding: 5px 10px;
    //     width: 95%;

    //     .el-input__prefix{
    //       padding-left: 5px;
    //     }
    //   }
    // }

    // .btn{
    //   position: absolute;
    //   top: 250px;
    //   left: 130px;
    // }
  }
}
</style>
